from sklearn.naive_bayes import  GaussianNB #使用sklearn自带数据集
from sklearn.preprocessing import StandardScaler  #特征缩放
import joblib as  jlb # 保存模型
import matplotlib
matplotlib.rcParams['font.sans-serif'] = ['KaiTi']
from data_format import x_train,y_train,x_cross,y_cross,x_test,y_test
from test_result_show import plot_roc,plot_roc_nb,pie_chart
import warnings
warnings.filterwarnings('ignore')  # "error", "ignore", "always", "default", "module" or "once"
import matplotlib.pyplot as  plt


def naive_bayyes_train(x_train,y_train,x_cross,y_cross,x_test,y_test):
    # 特征缩放
    sc = StandardScaler()
    x_train = sc.fit_transform(x_train)
    x_cross = sc.fit_transform(x_cross)
    x_test = sc.fit_transform(x_test)

    # 将最佳模型保存
    classfier = GaussianNB()
    classfier.fit(x_train, y_train)
    jlb.dump(classfier, './models/naive_bayes.pkl')

def NB_result(x_test,y_test):
    # 加载模型
    classfier = jlb.load('./models/naive_bayes.pkl')

    # 对测试集进行预测
    y_pred = classfier.predict(x_test)

    # 画出饼状图
    pie_chart(y_pred,'朴素贝叶斯模型(NB)')

    plot_roc_nb(x_test,y_test,y_pred,classfier,'朴素贝叶斯模型(NB)')  # 绘制ROC曲线并求出AUC值 以及 评估预测

    print('y_pred:{}'.format(y_pred))


if __name__ == '__main__':
    # naive_bayyes_train(x_train,y_train,x_cross,y_cross,x_test,y_test)
    # show_change_naive_bayes(c_list, accurancy_list )

    NB_result(x_test, y_test)
